using System;
using System.Configuration;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Catalyst.Caching
{
	public class SocketPoolElementCollection : ConfigurationElementCollection
	{
		public SocketPoolElement this[int index]
		{
			get
			{
				return (SocketPoolElement)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}

				BaseAdd(index, value);
			}
		}

		protected override ConfigurationElement CreateNewElement()
		{
			return new SocketPoolElement();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((SocketPoolElement)element).Name;
		}
	}

	[Serializable]
	public class SocketPoolElement : ConfigurationElement
	{
		[ConfigurationProperty("servers")]
		[ConfigurationCollection(typeof(ServerElementCollection),
			AddItemName = "add",
			ClearItemsName = "clear",
			RemoveItemName = "remove")]
		public ServerElementCollection Servers
		{
			get { return (ServerElementCollection)this["servers"]; }
		}

		[ConfigurationProperty("name")]
		public string Name
		{
			get { return this["name"].ToString(); }
		}

		[ConfigurationProperty("initConnections", DefaultValue = 3)]
		public int InitConnections
		{
			get { return (int)this["initConnections"]; }
		}

		[ConfigurationProperty("minConnections", DefaultValue = 3)]
		public int MinConnections
		{
			get { return (int)this["minConnections"]; }
		}

		[ConfigurationProperty("maxConnections", DefaultValue = 10)]
		public int MaxConnections
		{
			get { return (int)this["maxConnections"]; }
		}

		[ConfigurationProperty("maxThreadBusyTime", DefaultValue = 1000 * 60 * 5)]
		public int MaxThreadBusyTime
		{
			get { return (int)this["maxThreadBusyTime"]; }
		}

		[ConfigurationProperty("maxThreadIdleTime", DefaultValue = 1000 * 60 * 3)]
		public int MaxThreadIdleTime
		{
			get { return (int)this["maxThreadIdleTime"]; }
		}

		[ConfigurationProperty("socketConnectionTimeout", DefaultValue = 50)]
		public int SocketConnectionTimeout
		{
			get { return (int)this["socketConnectionTimeout"]; }
		}

		[ConfigurationProperty("socketTimeout", DefaultValue = 1000 * 10)]
		public int SocketTimeout
		{
			get { return (int)this["socketTimeout"]; }
		}

		[ConfigurationProperty("maintenanceSleep", DefaultValue = 1000 * 5)]
		public int MaintenanceSleep
		{
			get { return (int)this["maintenanceSleep"]; }
		}

		[ConfigurationProperty("failover", DefaultValue = true)]
		public bool Failover
		{
			get { return (bool)this["failover"]; }
		}

		[ConfigurationProperty("nagle", DefaultValue = false)]
		public bool Nagle
		{
			get { return (bool)this["nagle"]; }
		}
	}
}